<?php
/*
  Template Name: seccion
 */
?>
<?php
setlocale(LC_ALL, 'es_ES');
global $wpdb;
$bid = '';
$sqlGetPosts = "";
if (!isset($_REQUEST['blog_id'])) {
    echo '<div class="modulo_blogs">
                <div class="titulos_col_derecha">
                    <h4>BLOGS</h4>
                </div><div class="contenido_modulo_blogs">';
    $bid = 28;
    $tmp_SQL = "(SELECT post_content, post_title, post_author, post_modified_gmt, post_date_gmt, $bid as blog_id,guid  FROM wp_" . $bid . "_posts WHERE post_type = 'post' and post_status= 'publish ')";
    $sqlGetPosts .= $tmp_SQL;
    if (strlen($sqlGetPosts) > 0) {
        $sqlGetPosts .= " UNION ";
    }
    $bid = 29;
    $tmp_SQL = "(SELECT post_content, post_title, post_author, post_modified_gmt, post_date_gmt, $bid as blog_id,guid  FROM wp_" . $bid . "_posts WHERE post_type = 'post' and post_status= 'publish ')";
    $sqlGetPosts .= $tmp_SQL;
	if (strlen($sqlGetPosts) > 0) {
        $sqlGetPosts .= " UNION ";
    }
    $bid = 30;
    $tmp_SQL = "(SELECT post_content, post_title, post_author, post_modified_gmt, post_date_gmt, $bid as blog_id,guid  FROM wp_" . $bid . "_posts WHERE post_type = 'post' and post_status= 'publish ')";
    $sqlGetPosts .= $tmp_SQL."  ORDER BY post_modified_gmt DESC limit 3";
    $post_list = $wpdb->get_results($sqlGetPosts, ARRAY_A);
    $j = 0;
    if (is_array($post_list) && count($post_list) > 0) {
        foreach ($post_list as $post) {
            $title_post = $post["post_title"];
            $tmp_post_fecha = $post["post_modified_gmt"];
            $fecha_post = explode(":", $tmp_post_fecha);
            $varaux = explode("-", $fecha_post[0]);
            $varano = $varaux[0];
            //		echo $varano;
            $varmes = $varaux[1];
            //		echo $varmes;
            $vardia = $varaux[2];

            $vardi = explode(" ", $vardia);


            $id_author = $post["post_author"];
            $author = "";
            $datos=get_userdata($id_author);
            $author=$datos->data->display_name;		
            $bid = $post["blog_id"];

            $blog_details = get_blog_details($bid);
		$tmp_blog_name = $blog_details->blogname;
		$tmp_siteurl = $blog_details->siteurl."/";
            ?>



            <div class="un_blog">
                <a href="<?php echo $tmp_siteurl; ?>"><?php
            if ($id_author == '' || $id_author == 1) {
                echo '<img src="'.get_template_directory_uri().'/images/avatar_default_blogs_ee.jpg" height="67" width="100"/>';
            } else {

                echo userphoto__get_userphoto($id_author, 3, '', '', array('style' => 'width:100px; height:67px;'), get_template_directory_uri().'/images/avatar_default_blogs_ee.jpg', $tmp_siteurl . "files/");
            }
            ?></a>
                <h3><a href="<?php echo $tmp_siteurl; ?>"><?php echo $tmp_blog_name; ?></a></h3>
                <h4><a href="<?php echo $post['guid']; ?>"><?php echo $title_post; ?></a></h4>
                <h5><?php echo strftime("%e %B %Y", strtotime($varmes . "/" . $vardi[0] . "/" . $varano)); ?></h5>
                <div class="clear"></div>
            </div>

            <?php
        }
    }
    echo '</div><div class="clear"></div>
            </div>';
} else {
    $bid = $_REQUEST['blog_id'];
    if ($bid != 1) {
        $sqlGetPosts = "SELECT post_content, post_title, post_author, post_modified_gmt, post_date_gmt, $bid as blog_id,guid  FROM wp_" . $bid . "_posts WHERE post_type = 'post' and post_status= 'publish '";
    }
    $sqlGetPosts .= " ORDER BY post_modified_gmt DESC limit 3";
    $post_list = $wpdb->get_results($sqlGetPosts, ARRAY_A);
    $npost = count($post_list);
    $j = 0;
    if (is_array($post_list) && $npost > 0) {
        foreach ($post_list as $post) {
            $title_post = $post["post_title"];
            $tmp_post_fecha = $post["post_modified_gmt"];
            $fecha_post = explode(":", $tmp_post_fecha);
            $varaux = explode("-", $fecha_post[0]);
            $varano = $varaux[0];
            //		echo $varano;
            $varmes = $varaux[1];
            //		echo $varmes;
            $vardia = $varaux[2];

            $vardi = explode(" ", $vardia);


            $id_author = $post["post_author"];
            $author = "";
            $datos=get_userdata($id_author);
            $author=$datos->data->display_name;		

            //$section = get_posts_section($post["blog_id"]);

            $bid = $post["blog_id"];

            $blog_details = get_blog_details($bid);
	    $tmp_blog_name = $blog_details->blogname;
	    $tmp_siteurl = $blog_details->siteurl."/";

            if ($j == 0) {
                ?>
                <div class="newsItem blog">
                    <h2 class="moduleHeading01">
                                                        	BLOG <?php echo get_blog_option($bid, "blogdescription", ""); ?>
                    </h2>
                    <div class="blogList">
                        <ul>
                            <li>
                                <a href="<?php echo $tmp_siteurl; ?>">
                                    <?php
                                    if ($id_author == '' || $id_author == 1) {
                                        echo '<img src="'.get_template_directory_uri().'/images/avatar_default_blogs_ee.jpg" height="65" width="98"/>';
                                    } else {

                                        echo userphoto__get_userphoto($id_author, 3, '', '', array('style' => 'width:98px; height:65px;'), get_template_directory_uri().'/images/avatar_default_blogs_ee.jpg', $tmp_siteurl . "files/");
                                    }
                                    ?></a>
                                <div class="detail">
                                    <h2>
                                        <a href="<?php echo $post['guid']; ?>"><?php echo $title_post; ?></a>
                                        <span>
                <?php echo strftime("%e %B %Y", strtotime($varmes . "/" . $vardi[0] . "/" . $varano)); ?>
                                        </span>
                                    </h2>
                                    
                                </div>
                                <div class="clear"></div>
                            </li>
                        </ul>
                        <div class="clear"></div>

                    </div>
                </div>
                <?php
            } else {
                if ($j == 1)
                    echo '<div class="newsItem">
                    	<div class="otherPosts">
                            <h2>
                                 <a href="' . $tmp_siteurl . '">OTROS POSTS</a>
                            </h2>';
                ?>

                <p>
                    <a href="<?php echo  $post['guid']; ?>"><?php echo $title_post; ?></a>
                </p>


                <?php
                if ($j == ($npost - 1))
                    echo '</div>
                        <div class="clear"></div>
                    </div>';
            }
            $j++;
        }
    }
}?>